<?php
/**
 * Created by PhpStorm.
 * User: PC
 * Date: 2017/8/11
 * Time: 10:16
 */

namespace frontend\controllers;


use common\models\ActivityLotteryGuestsPrize;
use common\models\ActivityLotteryPrize;
use yii;
class GuestsActivityLotteryController extends PublicBaseController
{
    protected $general_prize_code;
    protected $everyday_free_type_code;
    protected $everyday_limit_type_code;
    protected $general_prize_huiwenbi_code;
    protected $general_prize_num_code;
    protected $activity_opend_code;

    public function init()
    {
        parent::init(); // TODO: Change the autogenerated stub

        $this->general_prize_code = 2;
        $this->general_prize_huiwenbi_code = 0;
        $this->general_prize_num_code = 1;

        $this->everyday_free_type_code = 0;
        $this->everyday_limit_type_code = 0;
        $this->activity_opend_code = 1;
    }

    /**
     * 访客大转盘奖品详情
     */
    public function actionActivityDetail(){
        $prize_list = ActivityLotteryGuestsPrize::find()
            ->select(['prize_id', 'goods_id', 'cover_img', 'info', 'percentage', 'instructions', 'prize_type'])
            ->asArray()->all();
        $prize_list || $prize_list = [];

        $this->_successData($prize_list);
    }

    /**
     * 访问抽奖
     */
    public function actionGetPrize(){
        $session_id = yii::$app->request->post('sid', 0);
        if(!$session_id) $this->_errorData('1000', '参数错误');

        $prize_list = ActivityLotteryGuestsPrize::find()
            ->alias('alp')
            ->select(['alp.*', 'algp.prize_name', 'algp.prize_type as general_prize_type'])
            ->leftJoin('vrshop.activity_lottery_general_prize algp', 'alp.general_id = algp.general_id')
            ->orderBy('percentage ASC')
            ->asArray()
            ->all();

        if(!$prize_list) $this->_errorData('2000', ['prize'=>0, 'prize_type'=>'', 'record_id'=>0, 'msg'=>'奖品信息错误']);

        $prize_arr = [];
        foreach ($prize_list as $key=>$item) {
            $prize_arr[$key]['id']    = $key + 1;
            $prize_arr[$key]['prize_id'] = $item['prize_id'];
            $prize_arr[$key]['prize'] = $item['info'];
            $prize_arr[$key]['v']     = $item['percentage'];
        }

        foreach ($prize_arr as $key => $val) {
            $arr[$val['id']] = $val['v'];
        }
        //$rid中奖的序列号码
        $rid = ActivityLotteryPrize::_getRand($arr);     //根据概率获取奖项id
        $prize_info = $prize_list[$rid - 1];             //中奖项

        //
        if($prize_info['prize_type'] == $this->general_prize_code){
            if($prize_info['general_prize_type'] == $this->general_prize_huiwenbi_code){
                $prize_info['prize_type'] = 2;                      //汇闻币
            }elseif($prize_info['general_prize_type'] == $this->general_prize_num_code){
                $prize_info['prize_type'] = 3;                      //抽奖次数
            }else{
                $prize_info['prize_type'] = 0;
            }
        }

        /* 添加抽奖记录 */
        $insert_record_ret = yii::$app->db->createCommand()->insert('vrshop.activity_lottery_record',[
            'activity_id' => '0',                        //访客抽奖  活动ID为零
            'session_id'  => $session_id,
            'user_id'     => '0',
            'play_type'   => '0',
            'cost_huiwenbi' => '0',
            'prize'         => $rid,
            'prize_name'    => $prize_info['info'],
            'prize_type'    => $prize_info['prize_type'],
            'prize_cover_img' => $prize_info['cover_img'],
            'create_time' => time(),
        ])->execute();
        if(!$insert_record_ret) $this->_errorData('2001', '添加抽奖记录失败');
        $record_id = yii::$app->db->getLastInsertId();

        $this->_successData(['prize'=>$rid, 'prize_type'=>$prize_info['prize_type'], 'record_id'=>$record_id]);
    }
}